/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.dao;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

import etcore.webapp.grouptalk.common.object.Attachment;
import etcore.webapp.grouptalk.common.object.Comment;
import etcore.webapp.grouptalk.common.object.Message;

/**
 * An instance of MessageDAO is a data access object used to access Message,
 * which contains query and update operations
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class MessageDAO extends BaseDAO {

	public int batchTopicTransforming(Integer sourceTopicId,
			Integer targetTopicId) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "update Message set tid=? where tid=? ";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, targetTopicId);
		queryObject.setParameter(1, sourceTopicId);

		return queryObject.executeUpdate();
	}

	@Override
	public void deleteSingle(Object persistentInstance) {
		Session session = HibernateSessionFactory.getSession();
		if (persistentInstance instanceof Message) {
			CommentDAO commentDAO = new CommentDAO();
			List<Comment> persistentInstanceList = commentDAO.findByProperty(
					"gmid", ((Message) persistentInstance).getId());
			commentDAO.deleteList(persistentInstanceList);

			AttachmentDAO attachmentDAO = new AttachmentDAO();
			List<Attachment> atts = attachmentDAO.findByProperty("gmid",
					((Message) persistentInstance).getId());
			attachmentDAO.deleteList(atts);

			MessageTagsDAO messageTagsDAO = new MessageTagsDAO();
			messageTagsDAO.deleteByProperty("mid",
					((Message) persistentInstance).getId());
		}

		session.delete(persistentInstance);

	}

	@SuppressWarnings("rawtypes")
	public List findActiveUsers(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select userName, count(userName) from gt_message where flag>=0  group by userName order by count(userName) desc";
		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;
	}

	public List<Message> findAll(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Message where flag>=0 ";
		Query queryObject = session.createQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Message> results = queryObject.list();

		return results;

	}

	public Message findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		Message instance = (Message) session.get(Message.class.getName(), id);

		return instance;

	}

	@SuppressWarnings("rawtypes")
	public List findByProperty(String propertyName, Object value, Integer up,
			Integer flag, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer
				.append("select id, tid, name, addtime, userName, num, up, flag, tag, source, stime from ");
		sqlStringBuffer.append(Message.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer
				.append("= ?  and model.up=? and model.flag=? order by model.addtime desc");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);
		queryObject.setParameter(1, up);
		queryObject.setParameter(2, flag);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findByPropertyAndTags(String propertyName, Object value,
			List<String> tagList, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String countStr = "" + tagList.size();
		StringBuffer tagIdBuffer = new StringBuffer();
		int number = 0;
		tagIdBuffer.append("(");
		for (String tag : tagList) {
			number++;
			if (number > 1)
				tagIdBuffer.append(",");
			tagIdBuffer.append(tag);

		}
		tagIdBuffer.append(")");
		String queryString = "select model.id, model.tid, model.name, model.addtime, model.userName, model.num, model.up, model.flag, model.tag, model.source, model.stime from gt_message as model, gt_messagetags as mtmodel where  model."
				+ propertyName
				+ "= ?    and model.flag>=0 and STRCMP(now(),model.addtime)>=0 and model.id= mtmodel.mid and mtmodel.tagid in"
				+ tagIdBuffer.toString()
				+ " group by  model.id having count(*) =" + countStr;

		queryString = queryString
				+ "  order by model.up desc,model.addtime desc";
		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, value);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findAllByProperty(String propertyName, Object value, int begin,
			int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select id, tid, name, addtime, userName, num, up, flag, tag, source, stime from Message as model where model."
				+ propertyName
				+ "= ? order by model.up desc,model.addtime desc";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findIDsByPropertyAndFlag(String propertyName, Object value,
			Integer flag, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select id  from Message as model where model."
				+ propertyName + "= ? and flag=? order by model.addtime desc";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setParameter(1, flag);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}
	@SuppressWarnings("rawtypes")
	public List findIDsByProperty (String propertyName, Object value,
			  int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select id  from Message as model where model."
				+ propertyName + "= ?   order by model.addtime desc";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);
 
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}
	@SuppressWarnings("rawtypes")
	public List findByProperty(String propertyName, Object value, int begin,
			int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select id, tid, name, addtime, userName, num, up, flag, tag, source, stime from Message as model where model."
				+ propertyName
				+ "= ?  and model.flag>=0 and STRCMP(now(),model.addtime)>=0 order by model.up desc,model.addtime desc";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	public long findCommentNum(Integer gmid) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Comment as model where model.gmid=? and STRCMP(now(),model.time)>=0";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, gmid);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	@SuppressWarnings("rawtypes")
	public List findEvents(boolean local, String startDate, String endDate,
			int begin, int length, String rankField, boolean desc) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";
		if (local)
			queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_topic,gt_group where gt_message.tid = gt_topic.id and gt_topic.gid=gt_group.id and   gt_group.open=4 and gt_group.local=1 and   gt_message.flag>=0 and (TO_DAYS('"
					+ startDate
					+ "')-TO_DAYS(gt_message.stime))<=0  and (TO_DAYS('"
					+ endDate
					+ "')-TO_DAYS(gt_message.stime))>0 order by  "
					+ rankField;
		else
			queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_topic,gt_group where gt_message.tid = gt_topic.id and gt_topic.gid=gt_group.id and   gt_group.open=4 and  gt_group.en=1 and  gt_message.flag>=0 and (TO_DAYS('"
					+ startDate
					+ "')-TO_DAYS(gt_message.stime))<=0  and (TO_DAYS('"
					+ endDate
					+ "')-TO_DAYS(gt_message.stime))>0 order by  "
					+ rankField;
		if (desc)
			queryString = queryString + " desc";
		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	public long findSizeOfEvents(boolean local, String startDate, String endDate) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";
		if (local)
			queryString = "select count(gt_message.id) as ct  from gt_message,gt_group,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and   gt_group.open=4 and gt_group.local=1 and  gt_message.flag>=0 and (TO_DAYS('"
					+ startDate
					+ "')-TO_DAYS(gt_message.stime))<=0  and (TO_DAYS('"
					+ endDate + "')-TO_DAYS(gt_message.stime))>0 ";

		else
			queryString = "select count(gt_message.id) as ct  from gt_message,gt_group,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and   gt_group.open=4  and gt_group.en=1 and  gt_message.flag>=0 and (TO_DAYS('"
					+ startDate
					+ "')-TO_DAYS(gt_message.stime))<=0  and (TO_DAYS('"
					+ endDate + "')-TO_DAYS(gt_message.stime))>0 ";

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);

		// queryObject.setParameter(0, g);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return results == null ? 0 : ((Number) results.get(0)).longValue();

	}

	public long findSizeOfEvents(String startDate, String endDate,
			Integer directoryID) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select count(gt_message.id) as ct  from gt_message,gt_group,gt_topic,gt_groupdirectoryrelationship where gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open=4 and gt_group.id=gt_groupdirectoryrelationship.gid and gt_groupdirectoryrelationship.did="
				+ directoryID
				+ " and  gt_message.flag>=0 and (TO_DAYS('"
				+ startDate
				+ "')-TO_DAYS(gt_message.stime))<=0  and (TO_DAYS('"
				+ endDate
				+ "')-TO_DAYS(gt_message.stime))>0 ";

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);

		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null || results.size() == 0) ? 0 : ((Number) results
				.get(0)).longValue();

	}

	public long findSizeOfEvents(String startDate, String endDate,
			String userName) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select count(gt_message.id) as ct  from gt_message,gt_group,gt_topic  where gt_message.userName='"
				+ userName
				+ "' and gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open=4 and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0  and  (TO_DAYS('"
				+ startDate
				+ "')-TO_DAYS(gt_message.stime))<=0  and (TO_DAYS('"
				+ endDate
				+ "')-TO_DAYS(gt_message.stime))>0 ";

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);

		// queryObject.setParameter(0, g);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null || results.size() == 0) ? 0 : ((Number) results
				.get(0)).longValue();

	}

	public long findSizeOfFavouriteEvents(String startDate, String endDate,
			String userName) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select count( model.id) as ct   from gt_message as model, gt_favourite as fmodel,gt_topic, gt_group where model.id=fmodel.mid and fmodel.userName='"
				+ userName
				+ "' and model.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open=4 and  and model.flag>=0 and STRCMP(now(),model.addtime)>=0 and (TO_DAYS('"
				+ startDate
				+ "')-TO_DAYS(gt_message.stime))<=0  and (TO_DAYS('"
				+ endDate
				+ "')-TO_DAYS(gt_message.stime))>0 ";

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);

		// queryObject.setParameter(0, g);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null || results.size() == 0) ? 0 : ((Number) results
				.get(0)).longValue();

	}

	@SuppressWarnings("rawtypes")
	public List findEvents(String startDate, String endDate, String userName,
			int begin, int length, String rankField, boolean desc) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id,  gt_message.tid,  gt_message.name,  gt_message.addtime,  gt_message.userName,  gt_message.num,  gt_message.up ,  gt_message.flag,  gt_message.tag,  gt_message.source,  gt_message.stime from gt_message,gt_topic,gt_group where gt_message.userName='"
				+ userName
				+ "'and gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open=4  and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and  (TO_DAYS('"
				+ startDate
				+ "')-TO_DAYS(gt_message.stime))<=0  and (TO_DAYS('"
				+ endDate
				+ "')-TO_DAYS(gt_message.stime))>0 order by  " + rankField;
		if (desc)
			queryString = queryString + " desc";
		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findEvents(String startDate, String endDate,
			Integer directoryID, int begin, int length, String rankField,
			boolean desc) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select  gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_group,gt_topic,gt_groupdirectoryrelationship where gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open=4 and gt_group.id=gt_groupdirectoryrelationship.gid and gt_groupdirectoryrelationship.did=?  and   gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and (TO_DAYS('"
				+ startDate
				+ "')-TO_DAYS(gt_message.stime))<=0  and (TO_DAYS('"
				+ endDate
				+ "')-TO_DAYS(gt_message.stime))>0 order by  " + rankField;
		if (desc)
			queryString = queryString + " desc";
		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, directoryID);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findFavouriteEvents(String startDate, String endDate,
			String userName, int begin, int length, String rankField,
			boolean desc) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select model.id, model.tid, model.name, model.addtime, model.userName, model.num, model.up , model.flag, model.tag, model.source, model.stime from gt_message as model, gt_favourite as fmodel,gt_topic, gt_group where model.id=fmodel.mid and fmodel.userName='"
				+ userName
				+ "' and model.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open=4 and  model.flag>=0 and STRCMP(now(),model.addtime)>=0 and (TO_DAYS('"
				+ startDate
				+ "')-TO_DAYS(gt_message.stime))<=0  and (TO_DAYS('"
				+ endDate
				+ "')-TO_DAYS(gt_message.stime))>0 order by  " + rankField;
		if (desc)
			queryString = queryString + " desc";
		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findFavouriteMessages(String userName, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer
				.append("select model.id, model.tid, model.name, model.addtime, model.userName, model.num, model.up, model.flag, model.tag,   model.source, model.stime, favourmodel.addTime from ");
		sqlStringBuffer.append(Message.class.getSimpleName());
		sqlStringBuffer
				.append(" as model, Favourite as favourmodel where model.id=favourmodel.mid and favourmodel.userName=? order by favourmodel.id desc");

		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, userName);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findFavouriteMessages(String userName, Integer open, int begin,
			int length) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer
				.append("select model.id, model.tid, model.name, model.addtime, model.userName, model.num, model.up, model.flag, model.tag,   model.source, model.stime, favourmodel.addTime from gt_message  as model, gt_favourite as favourmodel,gt_topic,gt_group where model.id=favourmodel.mid and favourmodel.userName=? and model.tid=gt_topic.id and gt_topic.gid=gt_group.id and    gt_group.open=?   and model.flag>=0  order by favourmodel.id desc");

		Query queryObject = session.createSQLQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, userName);
		queryObject.setParameter(1, open);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findFullRecordByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Message as model where model."
				+ propertyName + "= ?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		List results = queryObject.list();

		return results;

	}

	/**
	 * Find hottest English messages which belong to groups with the input open
	 * 
	 * 
	 * @param open
	 *            input open value of a group
	 * @param begin
	 *            start position
	 * @param length
	 * @return list of objects
	 */
	@SuppressWarnings("rawtypes")
	public List findHottestEnglishMessagesByOpen(int open, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_group,gt_topic where  gt_message.tid= gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open="
				+ open
				+ " and gt_group.en=1 and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 order by gt_message.num  desc ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findHottestPublicEnglishMessages(int days, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_group,gt_topic where (TO_DAYS(now())-TO_DAYS(gt_message.addtime))<"
				+ days
				+ " and gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open>0 and gt_group.func=0 and gt_group.en=1   and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 order by gt_message.num  desc ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	/**
	 * Find hottest messages of local language which belong to groups with the
	 * input open
	 * 
	 * 
	 * @param open
	 *            input open value of a group
	 * @param begin
	 *            start position
	 * @param length
	 * @return list of objects
	 */
	@SuppressWarnings("rawtypes")
	public List findHottestLocalMessagesByOpen(int open, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_group,gt_topic where gt_group.open="
				+ open
				+ " and gt_group.local=1  and  gt_topic.gid=gt_group.id and  gt_message.tid=gt_topic.id and  gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 order by gt_message.num  desc ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findHottestPublicLocalMessages(int days, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_group,gt_topic where (TO_DAYS(now())-TO_DAYS(gt_message.addtime))<"
				+ days
				+ " and  gt_group.open>0 and gt_group.func=0 and gt_group.local=1 and gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0  order by gt_message.num  desc ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findHottestPublicMessages(int days, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_group,gt_topic where (TO_DAYS(now())-TO_DAYS(gt_message.addtime))<"
				+ days
				+ " and  gt_group.open>0 and gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_message.flag>=0 order by gt_message.num  desc ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	public Comment findLastComment(Integer gmid) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Comment as model where model.gmid=? and STRCMP(now(),model.time)>=0 order by model.id desc ";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, gmid);
		queryObject.setFirstResult(0);
		queryObject.setMaxResults(1);
		Comment result = (Comment) queryObject.uniqueResult();

		return result;

	}

	@SuppressWarnings("rawtypes")
	public List findMessages(String propertyName, Object value, Integer open,
			int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_group,gt_topic  where gt_message."
				+ propertyName
				+ "=? and  gt_group.open=?  and gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 order by gt_message.addtime desc ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setParameter(1, open);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findMessages(String propertyName, Object value, Integer open,
			Integer nav, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_group,gt_topic  where gt_message."
				+ propertyName
				+ "=?   and gt_group.open=? and gt_group.nav=? and gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 order by gt_message.addtime desc ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setParameter(1, open);
		queryObject.setParameter(2, nav);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findMsgsByGid(Integer g, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_topic  where  gt_message.tid=gt_topic.id and gt_topic.gid="
				+ g
				+ "  and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 order by  gt_message.up desc,gt_message.addtime desc ";

		Query queryObject = session.createSQLQuery(queryString);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findByGidOrderByClick(Integer g, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_topic  where  gt_message.tid=gt_topic.id and gt_topic.gid="
				+ g
				+ "  and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 order by  gt_message.num desc ";

		Query queryObject = session.createSQLQuery(queryString);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findMsgsByGidAndTags(Integer g, List<String> tagList,
			int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String countStr = "" + tagList.size();
		StringBuffer tagIdBuffer = new StringBuffer();
		int number = 0;
		tagIdBuffer.append("(");
		for (String tag : tagList) {
			number++;
			if (number > 1)
				tagIdBuffer.append(",");
			tagIdBuffer.append(tag);

		}
		tagIdBuffer.append(")");
		String queryString = "";

		queryString = "select model.id, model.tid, model.name, model.addtime, model.userName, model.num, model.up, model.flag, model.tag, model.source, model.stime from gt_message as model,gt_messagetags as mtmodel, gt_topic where  model.tid=gt_topic.id and gt_topic.gid="
				+ g
				+ "  and model.flag>=0 and STRCMP(now(),model.addtime)>=0 and model.id =mtmodel.mid and mtmodel.tagid in"
				+ tagIdBuffer.toString()
				+ " group by model.id having count(*) =" + countStr;

		queryString = queryString + " order by model.up desc, model.addtime desc";
		Query queryObject = session.createSQLQuery(queryString);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findPublicEnglishMessages(int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_group,gt_topic  where gt_group.open>0 and gt_group.func=0 and gt_group.en=1 and gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 order by gt_message.addtime desc,gt_message.name desc ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findPublicLocalMessages(int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_group,gt_topic  where gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open>0 and gt_group.local=1 and gt_group.func=0  and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 order by gt_message.addtime desc,gt_message.name desc ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findPublicMessages(int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_group,gt_topic  where gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open>0   and gt_message.flag>=0 order by gt_message.addtime desc ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findPublicMessagesByInterval(String startDate, String endDate,
			int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id,  gt_message.tid,  gt_message.name,  gt_message.addtime,  gt_message.userName,  gt_message.num,  gt_message.up ,  gt_message.flag,  gt_message.tag,  gt_message.source,  gt_message.stime from gt_message,gt_topic,gt_group where gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open>=0  and gt_group.nav=0  and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and  (TO_DAYS('"
				+ startDate
				+ "')-TO_DAYS(gt_message.addtime))<=0  and (TO_DAYS('"
				+ endDate
				+ "')-TO_DAYS(gt_message.addtime))>=0 order by gt_message.addtime desc ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findPublicMessages(String propertyName, Object value,
			int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_group,gt_topic where gt_message."
				+ propertyName
				+ "=? and  gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open>0   and gt_message.flag>=0 order by gt_message.addtime desc ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findRecentActiveUsers(Integer days, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select gt_message.userName,count(gt_message.userName),gt_user.avatar  from gt_group,gt_topic,gt_message,gt_user where gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open>0 and gt_user.userName=gt_message.userName and (TO_DAYS(now())-TO_DAYS(gt_message.addtime))<"
				+ days
				+ "  and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 group by gt_message.userName order by count(gt_message.userName) desc";

		Query queryObject = session.createSQLQuery(queryString);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;
	}

	@SuppressWarnings("rawtypes")
	public List findSegments(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select id, tid, name, addtime, userName, num, up, flag, tag, source, stime  from gt_message   order by stime desc, addtime desc ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;
	}

	@SuppressWarnings("rawtypes")
	public List findSharedFiles(Integer g, Integer flag, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_topic where  gt_message.flag="
				+ flag
				+ " and gt_message.tid=gt_topic.id and  gt_topic.gid="
				+ g + "  order by gt_message.addtime desc  ";

		Query queryObject = session.createSQLQuery(queryString);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findSharedFiles(String userName, Integer flag, int begin,
			int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select id, tid, name, addtime, userName, num, up, flag, tag, source, stime from gt_message where userName='"
				+ userName + "' and flag=" + flag + "  order by addtime desc ";

		Query queryObject = session.createSQLQuery(queryString);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findSharedFiles(String userName, Integer g, Integer flag,
			int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_topic where gt_message.userName='"
				+ userName
				+ "' and gt_message.flag="
				+ flag
				+ " and gt_message.tid=gt_topic.id and gt_topic.gid="
				+ g
				+ "  order by gt_message.addtime desc";

		Query queryObject = session.createSQLQuery(queryString);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	public long findSizeByDaysAndGid(Integer days, Integer g) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select count(gt_message.id) as ct from gt_message,gt_topic where gt_message.tid=gt_topic.id and  gt_topic.gid="
				+ g
				+ "   and  (TO_DAYS(now())-TO_DAYS(gt_message.addtime))<?  and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0";

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);
		sqlQuery.setParameter(0, days);
		// queryObject.setParameter(0, g);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null || results.size() == 0) ? 0 : ((Number) results
				.get(0)).longValue();

	}

	public long findSizeOfPublicMessagesByInterval(String startDate,
			String endDate) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(gt_message.id) as ct  from gt_message,gt_group,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and   gt_group.open>=0 and gt_group.nav=0  and  gt_message.flag>=0 and (TO_DAYS('"
				+ startDate
				+ "')-TO_DAYS(gt_message.addtime))<=0  and (TO_DAYS('"
				+ endDate + "')-TO_DAYS(gt_message.addtime))>=0 ";
		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);

		// queryObject.setParameter(0, g);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null || results.size() == 0) ? 0 : ((Number) results
				.get(0)).longValue();

	}

	public long findSizeByFutureDaysAndTid(Integer days, Integer tid) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select count(gt_message.id) as ct from gt_message,gt_topic where gt_message.tid=gt_topic.id and  gt_topic.id="
				+ tid
				+ "   and  (TO_DAYS(now())-TO_DAYS(gt_message.addtime))=?  and gt_message.flag>=0";

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);
		sqlQuery.setParameter(0, days);
		// queryObject.setParameter(0, g);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null || results.size() == 0) ? 0 : ((Number) results
				.get(0)).longValue();

	}

	public long findSizeByFutureDaysAndTid(String inputDate, Integer days,
			Integer tid) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select count(gt_message.id) as ct from gt_message,gt_topic where gt_message.tid=gt_topic.id and  gt_topic.id="
				+ tid
				+ "   and  (TO_DAYS('"
				+ inputDate
				+ "')-TO_DAYS(gt_message.addtime))=?  and gt_message.flag>=0";

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);
		sqlQuery.setParameter(0, days);
		// queryObject.setParameter(0, g);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null || results.size() == 0) ? 0 : ((Number) results
				.get(0)).longValue();

	}

	@SuppressWarnings("rawtypes")
	public List findByFutureDaysAndTid(String inputDate, Integer days,
			Integer tid, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select gt_message.id   from gt_message,gt_topic where gt_message.tid=gt_topic.id and  gt_topic.id="
				+ tid
				+ "   and  (TO_DAYS('"
				+ inputDate
				+ "')-TO_DAYS(gt_message.addtime))=?  and gt_message.flag>=0";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, days);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);

		List results = queryObject.list();

		return results;

	}

	public long findSizeByGid(Integer g) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";
		queryString = "select count(gt_message.id) as ct from gt_message,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid="
				+ g
				+ "  and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0";

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);

		// queryObject.setParameter(0, g);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null || results.size() == 0) ? 0 : ((Number) results
				.get(0)).longValue();

	}

	public long findSizeByGidAndTags(Integer g, List<String> tagList) {

		Session session = HibernateSessionFactory.getSession();
		String countStr = "" + tagList.size();
		StringBuffer tagIdBuffer = new StringBuffer();
		int number = 0;
		tagIdBuffer.append("(");
		for (String tag : tagList) {
			number++;
			if (number > 1)
				tagIdBuffer.append(",");
			tagIdBuffer.append(tag);

		}
		tagIdBuffer.append(")");
		String queryString = "";
		queryString = "select count(*) from (select  model.id   from gt_message as model, gt_topic as tmodel,gt_messagetags as mtmodel where model.tid=tmodel.id and  tmodel.gid="
				+ g
				+ "  and model.flag>=0 and STRCMP(now(),model.addtime)>=0 and model.id = mtmodel.mid  and  mtmodel.tagid in"
				+ tagIdBuffer.toString()
				+ " group by model.id having count(*) =" + countStr+") as t";

		Query queryObject = session.createSQLQuery(queryString);
		 
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findSizeByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Message as model where model."
				+ propertyName
				+ "= ? and model.flag>=0 and STRCMP(now(),model.addtime)>=0";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findAllSizeByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Message as model where model."
				+ propertyName + "= ? ";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findSizeByProperty(String propertyName, Object value,
			Integer up, Integer flag) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Message as model where model."
				+ propertyName
				+ "= ? and model.up=? and model.flag=? order by model.addtime desc";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setParameter(1, up);
		queryObject.setParameter(2, flag);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findSizeByPropertyAndFlag(String propertyName, Object value,
			Integer flag) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Message as model where model."
				+ propertyName + "= ?  and model.flag=? ";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		queryObject.setParameter(1, flag);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findSizeByPropertyAndTags(String propertyName, Object value,
			List<String> tagList) {
		Session session = HibernateSessionFactory.getSession();
		String countStr = "" + tagList.size();
		StringBuffer tagIdBuffer = new StringBuffer();
		int number = 0;
		tagIdBuffer.append("(");
		for (String tag : tagList) {
			number++;
			if (number > 1)
				tagIdBuffer.append(",");
			tagIdBuffer.append(tag);

		}
		tagIdBuffer.append(")");
		String queryString = "select count(*) from ( select count(model.id) from gt_message as model,gt_messagetags as mtmodel  where model."
				+ propertyName
				+ "= ? and model.flag>=0 and STRCMP(now(),model.addtime)>=0 and model.id=mtmodel.mid and  mtmodel.tagid in"
				+ tagIdBuffer.toString()
				+ " group by model.id having count(*) =" + countStr+") as t";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, value);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findSizeOfFavouriteMessages(String userName) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select count(gt_message.id) as ct from gt_message,gt_favourite where gt_message.id=gt_favourite.mid and gt_favourite.userName='"
				+ userName + "'";

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);

		// queryObject.setParameter(0, g);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null || results.size() == 0) ? 0 : ((Number) results
				.get(0)).longValue();

	}

	public long findSizeOfFavouriteMessages(String userName, Integer open) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select count(gt_message.id) as ct from gt_message, gt_favourite,gt_topic,gt_group  where gt_message.id=gt_favourite.mid and gt_favourite.userName='"
				+ userName
				+ "'  and gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id  and gt_group.open="
				+ open + " and gt_message.flag>=0 ;";

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);

		// queryObject.setParameter(0, g);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null || results.size() == 0) ? 0 : ((Number) results
				.get(0)).longValue();

	}

	public long findSizeOfMessages(String propertyName, Object value,
			Integer open) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select count(gt_message.id) from gt_message,gt_topic,gt_group where gt_message."
				+ propertyName
				+ "=? and gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and   gt_group.open=?  and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setParameter(1, open);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findSizeOfMessages(String propertyName, Object value,
			Integer open, Integer nav) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select count(gt_message.id) from gt_message,gt_topic,gt_group where gt_message."
				+ propertyName
				+ "=? and gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open=? and gt_group.nav=? and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setParameter(1, open);
		queryObject.setParameter(2, nav);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findSizeOfPublicMessages(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(gt_message.id) from gt_message,gt_topic,gt_group where gt_message."
				+ propertyName
				+ "=? and gt_message.tid=gt_topic.id and gt_topic.gid=gt_group.id and gt_group.open>0  and gt_message.flag>=0  and STRCMP(now(),gt_message.addtime)>=0";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, value);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findSizeOfSharedFiles(Integer g, Integer flag) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(gt_message.id) as ct from gt_message,gt_topic where  gt_message.flag="
				+ flag
				+ " and gt_message.tid=gt_topic.id and gt_topic.gid="
				+ g;

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);

		// queryObject.setParameter(0, g);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null || results.size() == 0) ? 0 : ((Number) results
				.get(0)).longValue();

	}

	public long findSizeOfSharedFiles(String userName, Integer flag) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) as ct from gt_message where userName='"
				+ userName + "' and flag= " + flag;

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);

		// queryObject.setParameter(0, g);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null || results.size() == 0) ? 0 : ((Number) results
				.get(0)).longValue();

	}

	public long findSizeOfSharedFiles(String userName, Integer group,
			Integer flag) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(gt_message.id) as ct from gt_message,gt_topic where  gt_message.userName='"
				+ userName
				+ "' and gt_message.flag="
				+ flag
				+ " and gt_message.tid=gt_topic.id and gt_topic.gid=" + group;

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);

		// queryObject.setParameter(0, g);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null || results.size() == 0) ? 0 : ((Number) results
				.get(0)).longValue();

	}

	@SuppressWarnings("rawtypes")
	public List findTemplates(String propertyName, Object value, int begin,
			int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Message as model where model."
				+ propertyName
				+ "= ?  and model.flag=100 order by model.addtime desc";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	public Integer findTid(Integer gmid) {
		Session session = HibernateSessionFactory.getSession();

		String queryString = "select tid from Message as model where model.id=?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, gmid);

		Object results = queryObject.uniqueResult();

		return (results == null) ? null : ((Integer) results);

	}

	@SuppressWarnings("rawtypes")
	public List findTopByDaysAndGid(Integer days, Integer g, int begin,
			int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid="
				+ g
				+ "   and  (TO_DAYS(now())-TO_DAYS(gt_message.addtime))<?  and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 order by  gt_message.up desc,gt_message.addtime desc ";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, days);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List searchByPropertyAndGid(String propertyName, String value,
			Integer g, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";
		if (propertyName != null && propertyName.equals("etag"))
			queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid="
					+ g
					+ " and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and MATCH(gt_message."
					+ propertyName
					+ ") AGAINST ('"
					+ value
					+ "' IN BOOLEAN MODE)  order by gt_message.stime desc,gt_message.addtime desc";
		else if (propertyName != null && propertyName.equals("all"))
			queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid="
					+ g
					+ " and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and  gt_message.userName='"
					+ value
					+ "' or gt_message.name like '%"
					+ value
					+ "%' or gt_message.addTime like '%"
					+ value
					+ "%' or gt_message.content like '%"
					+ value
					+ "%'    order by gt_message.stime desc,ge_message.addtime desc";
		else if (propertyName != null && propertyName.equals("name&tag"))
			queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid="
					+ g
					+ " and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and  gt_message.name like '%"
					+ value
					+ "%' or gt_message.tag like '%"
					+ value
					+ "%'   order by gt_message.stime desc,gt_message.addtime desc";
		else
			queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid="
					+ g
					+ " and  gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and  gt_message."
					+ propertyName
					+ " like '%"
					+ value
					+ "%'  order by gt_message.stime desc,gt_message.addtime desc";
		Query queryObject = session.createSQLQuery(queryString);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List searchByPropertyAndTid(String propertyName, String value,
			Integer g, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();

		String queryString = "";
		if (propertyName != null && propertyName.equals("etag"))
			queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid="
					+ g
					+ " and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and MATCH(gt_message."
					+ propertyName
					+ ") AGAINST ('"
					+ value
					+ "' IN BOOLEAN MODE) order by gt_message.stime desc,gt_message.addtime desc";
		else if (propertyName != null && propertyName.equals("name&tag"))
			queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid="
					+ g
					+ " and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and (gt_message.name like '%"
					+ value + "%' or gt_message.tag like '%" + value + "%'  )";
		else
			queryString = "select gt_message.id, gt_message.tid, gt_message.name, gt_message.addtime, gt_message.userName, gt_message.num, gt_message.up , gt_message.flag, gt_message.tag, gt_message.source, gt_message.stime from gt_message,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid="
					+ g
					+ " and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and gt_message."
					+ propertyName
					+ " like '%"
					+ value
					+ "%'  order by gt_message.stime desc,gt_message.addtime desc";

		Query queryObject = session.createSQLQuery(queryString);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List searchByPropertyAndUserName(String propertyName, String value,
			String userName, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();

		String queryString = "";
		if (propertyName != null && propertyName.equals("name&tag"))
			queryString = "select id, tid, name, addtime, userName, num, up, flag, tag, source, stime from gt_message where userName='"
					+ userName
					+ "' and flag>=0 and STRCMP(now(),addtime)>=0 and ( name like '%"
					+ value + "%' or tag like '%" + value + "%' ) ";
		else
			queryString = "select id, tid, name, addtime, userName, num, up, flag, tag, source, stime from gt_message where userName='"
					+ userName
					+ "'  and flag>=0 and STRCMP(now(),addtime)>=0 and "
					+ propertyName
					+ " like '%"
					+ value
					+ "%'  order by  addtime desc";

		Query queryObject = session.createSQLQuery(queryString);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List searchFavouriteMessages(String userName, String propertyName,
			Object value, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer
				.append("select model.id, model.tid, model.name, model.addtime, model.userName, model.num, model.up, model.flag, model.tag,   model.source, model.stime, favourmodel.addTime from ");
		sqlStringBuffer.append(Message.class.getSimpleName());
		sqlStringBuffer
				.append(" as model, Favourite as favourmodel where model.id=favourmodel.mid and favourmodel.userName=?");

		sqlStringBuffer.append(" and model.flag>=0 and model.");

		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append(" like '%");
		sqlStringBuffer.append(value);
		sqlStringBuffer.append("%'  order by  model.addtime desc");

		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, userName);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	public long searchSizeByPropertyAndGid(String propertyName, String value,
			Integer g) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";
		if (propertyName != null && propertyName.equals("etag"))
			queryString = "select count(gt_message.id) as ct from gt_message,gt_topic where MATCH(gt_message."
					+ propertyName
					+ ") AGAINST ('"
					+ value
					+ "' IN BOOLEAN MODE) and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and gt_message.tid=gt_topic.id and gt_topic.gid="
					+ g;

		else if (propertyName != null && propertyName.equals("all"))
			queryString = "select count(gt_message.id) as ct from gt_message,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid="
					+ g
					+ " and gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and  gt_message.userName='"
					+ value
					+ "' or gt_message.name like '%"
					+ value
					+ "%' or gt_message.addTime like '%"
					+ value
					+ "%' or gt_message.content like '%" + value + "%'   ";
		else if (propertyName != null && propertyName.equals("name&tag"))
			queryString = "select count(gt_message.id) as ct from gt_message,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid="
					+ g
					+ "   and gt_message.flag>=0  and STRCMP(now(),gt_message.addtime)>=0 and gt_message.name like '%"
					+ value + "%' or gt_message.tag like '%" + value + "%'  ";
		else
			queryString = "select count(gt_message.id) as ct from gt_message,gt_topic where gt_message.tid=gt_topic.id and gt_topic.gid="
					+ g
					+ "   and   gt_message.flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and gt_message."
					+ propertyName + " like '%" + value + "%'  ";

		Query queryObject = session.createSQLQuery(queryString);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long searchSizeByPropertyAndTid(String propertyName, String value,
			Integer tid) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";
		if (propertyName != null && propertyName.equals("etag"))
			queryString = "select count(*) as ct from gt_message where tid="
					+ tid
					+ " and flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and MATCH("
					+ propertyName + ") AGAINST ('" + value
					+ "' IN BOOLEAN MODE)";

		else if (propertyName != null && propertyName.equals("name&tag"))
			queryString = "select count(*) as ct from gt_message where tid="
					+ tid
					+ " and flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and ( name like '%"
					+ value + "%' or tag like '%" + value + "%'  ";
		else
			queryString = "select count(*) as ct from gt_message where tid="
					+ tid
					+ " and flag>=0 and STRCMP(now(),gt_message.addtime)>=0 and "
					+ propertyName + " like '%" + value + "%' ";
		Query queryObject = session.createSQLQuery(queryString);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long searchSizeByPropertyAndUserName(String propertyName,
			String value, String userName) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";
		if (propertyName != null && propertyName.equals("name&tag"))
			queryString = "select count(*) as ct from gt_message where userName='"
					+ userName
					+ "' and flag>=0 and ( name like '%"
					+ value
					+ "%' or tag like '%" + value + "%' ) ";
		else
			queryString = "select count(*) as ct from gt_message where userName='"
					+ userName
					+ "' and flag>=0 and "
					+ propertyName
					+ " like '%" + value + "%' ";

		Query queryObject = session.createSQLQuery(queryString);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long searchSizeOfFavouriteMessages(String userName,
			String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select count(gt_message.id) as ct from gt_message,gt_favourite where gt_message.id=gt_favourite.mid  and gt_favourite.userName='"
				+ userName
				+ "'  and "
				+ propertyName
				+ " like '%"
				+ value
				+ "%'";
		Query queryObject = session.createSQLQuery(queryString);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}
}